//
//  HomeViewController.m
//  yunyan
//
//  Created by haichuang on 2018/2/11.
//  Copyright © 2018年 haichuang. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeTopCell.h"
//#import "HomeNewsCell.h"
#import "NewsHeaderView.h"
#import "HomeCategoryVC.h"
#import "BannerScrollViewTime.h"
#import "ActiveDetailViewController.h"
//#import "HomeNewsDetailViewController.h"
#import "NewsDetailsViewController.h"
#import "AppDelegate.h"

#import "SHNesHomeTableViewCell.h"

#import "MainTabBarController.h"

@interface HomeViewController ()<UITableViewDelegate, UITableViewDataSource,SDCycleScrollViewDelegate>
{
      NSArray *bannerArr;
    
    int page;
    
    NSMutableArray * _dataArray;
    NSArray * categoryArray;
    
    NSString * typeStr;
}



@property (nonatomic, strong) UITableView *bottomTab;
@property (nonatomic, strong) NSString *navTitle;
@end

@implementation HomeViewController
//
////视图将要显示时隐藏
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar setBackgroundImage:ImageName(@"矩形10") forBarMetrics:UIBarMetricsDefault];
//    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    
    [self getCategoryDataRequest];
    [self getBannerDataRequest];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.bottomTab  reloadData];
    });
}
//
////视图将要消失时取消隐藏
//-(void)viewWillDisappear:(BOOL)animated
//{
//    [super viewWillDisappear:animated];
//    
//    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
//}


- (void)viewDidLoad {
    [super viewDidLoad];
    typeStr = @"hot";
    page = 1;
    _dataArray = @[].mutableCopy;
    self.view.backgroundColor = [UIColor whiteColor];
   [self customNaviItemTitle:nil isLeft:NO];
    
      [self customTabBarButtonTitle:nil image:[UIImage imageNamed:@"个人中心"] target:self action:@selector(centerBtn) isLeft:YES];
    
      [self customTabBarButtonTitle:nil image:[UIImage imageNamed:@"随手拍"] target:self action:@selector(photoBtn) isLeft:NO];
 
    [self createUIView];
    
    [self getMainListRequest];
    
}

-(void)centerBtn {
    
    self.tabBarController.selectedIndex = 3;
}

-(void)photoBtn{
    
    self.tabBarController.selectedIndex = 2;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)createUIView{
 
    //创建顶部 nav的titleview;
    UIImageView * titleImaeg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 94, 25)];

    titleImaeg.image = [UIImage imageNamed:@"幸福云岩_nav"];
    self.navigationItem.titleView = titleImaeg;
    WeakSelf;
     _bottomTab = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [self.view addSubview:_bottomTab];
    [_bottomTab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.equalTo (weakSelf.view);
        
        
    }];
    self.bottomTab.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.bottomTab.backgroundColor = [UIColor whiteColor];
        _bottomTab.delegate = self;
        _bottomTab.dataSource = self;
    [self.bottomTab registerClass:[HomeTopCell class] forCellReuseIdentifier:@"HomeTopCell"];
    
  
    
    [self.bottomTab registerNib:[UINib nibWithNibName:@"SHNesHomeTableViewCell" bundle:nil] forCellReuseIdentifier:@"SHNesHomeTableViewCell"];
  //  self.bottomTab.backgroundColor = [UIColor whiteColor];

    
    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        page =1;
        [self getCategoryDataRequest];
        [self getBannerDataRequest];
        [self getMainListRequest];
        
    }];
    self.bottomTab.mj_header =header;
    MJRefreshAutoNormalFooter *  footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
          page++;
          [self getMainListRequest];
    }];
    
    footer.automaticallyRefresh = YES;
    self.bottomTab.mj_footer =footer;
    self.bottomTab.mj_footer.hidden = NO;
    
}


# pragma mark- UITableViewDelegate, UITableViewDataSource


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return nil;
        
    }else{
    
    
    NewsHeaderView * headerView = [[NewsHeaderView alloc] init];
        
        if ([typeStr isEqualToString:@"hot"]) {
            
             [headerView.hotBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
             [headerView.commentBtn setTitleColor:RGBA(153, 153, 153, 1) forState:UIControlStateNormal];
        }else{
            [headerView.hotBtn setTitleColor:RGBA(153, 153, 153, 1) forState:UIControlStateNormal];
            [headerView.commentBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        }
   
        
    headerView.hotBtnBlock =  ^(void){
        
        typeStr = @"hot";
        page = 1;
        [self getMainListRequest];
        NSLog(@"点击热门Btn");
    };

    headerView.commentBtnBlock = ^(void){
        
         typeStr = @"comment";
         page = 1;
        [self getMainListRequest];
         NSLog(@"点击评论Btn");
    };


    return headerView;
    
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
   
        return nil;
        
   
    
}
    
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return CGFLOAT_MIN;
        
    }else{
      return 84./2.;
        
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
 
    if (section == 0) {
        
        return 1;
    }
    else{
         return _dataArray.count;
    }
   
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    if (indexPath.section == 0) {
        
        return ScreenWidth*7/15 +120;
    }
    else{
        
        NSDictionary * dic = _dataArray[indexPath.row];
        
        NSArray * imageArray = dic[@"cover_pic"];
        
        if (imageArray.count!=1) {
        
            
            UILabel * titleLabel  = [[UILabel alloc]initWithFrame:CGRectMake(10, 15, WIDTH -20, 20)];
            titleLabel.text = dic[@"title"];
            titleLabel.font = [UIFont systemFontOfSize:16];
            titleLabel.numberOfLines = 2;
            [titleLabel sizeToFit];
            
            CGFloat widthImage = (WIDTH -40)/3;
            UIImageView * image;
            if (imageArray.count==0) {
                
                image = [[UIImageView alloc]initWithFrame:CGRectMake(10,CGRectGetMaxY(titleLabel.frame)+10 ,10 ,1)];
            }else {
                
                for (int i = 0; i<imageArray.count; i++) {
                    
                    image = [[UIImageView alloc]initWithFrame:CGRectMake(10+widthImage*i+10*i, CGRectGetMaxY(titleLabel.frame)+10, widthImage, widthImage *0.6)];
                    [image sd_setImageWithURL:ImageUrl(imageArray[i])];
                    image.layer.cornerRadius = 3.f;
                    image.layer.masksToBounds = YES;
                  
                }
                
            }
            
            return CGRectGetMaxY(image.frame)+40;
            
        }else{
            
             return 100.0f;
        }
       
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WeakSelf;
    if (indexPath.section == 0) {
        
        
        static NSString * cellid = @"cellid";
        
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        if (cell==nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellid];
        }else {
            
            while ([cell.contentView.subviews lastObject] !=nil) {
                
                [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
            }
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        NSArray * _bannerArr = bannerArr;
        
        NSMutableArray * imageArray  = @[].mutableCopy;
        NSMutableArray * imageTitlrArray  = @[].mutableCopy;
        if (imageArray) {
            [imageTitlrArray removeAllObjects];
            [imageArray removeAllObjects];
        }
        
        for (NSDictionary * dic in _bannerArr) {
            
            [imageArray addObject:dic[@"pic_url"]];
            [imageTitlrArray  addObject:dic[@"title"]];
        }
        
        UIView * topView =[[UIView alloc]initWithFrame:CGRectMake(0, 10, WIDTH, ScreenWidth*7/15)];
        topView.backgroundColor =[UIColor groupTableViewBackgroundColor];
        SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, WIDTH, ScreenWidth*7/15) delegate:self placeholderImage:[UIImage imageNamed:@"img_2"]];
        cycleScrollView.backgroundColor = [UIColor whiteColor] ;
        cycleScrollView.autoScrollTimeInterval = 3;
        cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
//        cycleScrollView.titlesGroup = imageTitlrArray;
                cycleScrollView.currentPageDotColor = [UIColor redColor];
        cycleScrollView.imageURLStringsGroup = imageArray;
        [topView addSubview:cycleScrollView];
        cycleScrollView.clickItemOperationBlock = ^(NSInteger index) {
            NSDictionary * dic = _bannerArr[index];
            
            if ([dic[@"type"] intValue]==1) {
                
                NewsDetailsViewController * homeDetiel = [[NewsDetailsViewController alloc]init];
                homeDetiel.news_idStr = [NSString stringWithFormat:@"%ld",[dic[@"href_url"] integerValue]];
                homeDetiel.hidesBottomBarWhenPushed = YES;
                
                [self.navigationController pushViewController:homeDetiel animated:YES];
            }else if ([dic[@"type"] intValue]==2){
                
                NSLog(@"站外新闻");
                NSString * urlStr = dic[@"href_url"];
                NSURL *url = [NSURL URLWithString:urlStr];
                if([[UIDevice currentDevice].systemVersion floatValue] >= 10.0){
                    if ([[UIApplication sharedApplication] respondsToSelector:@selector(openURL:options:completionHandler:)]) {
                        [[UIApplication sharedApplication] openURL:url options:@{}
                                                 completionHandler:^(BOOL success) {
                                                     NSLog(@"Open %d",success);
                                                 }];
                    } else {
                        BOOL success = [[UIApplication sharedApplication] openURL:url];
                        NSLog(@"Open  %d",success);
                    }
                    
                } else{
                    bool can = [[UIApplication sharedApplication] canOpenURL:url];
                    if(can){
                        [[UIApplication sharedApplication] openURL:url];
                    }
                }
                
            }else {
                NSLog(@"随手拍");
                
                  [self getActDetailsRequestForid:[NSString stringWithFormat:@"%ld",[dic[@"href_url"] integerValue]]];
            }
            
            
        };
        [topView addSubview:cycleScrollView];
        [cell.contentView addSubview:topView];
        
        
        //中间的分类:知、行、合、一
        UIView * minView = [[UIView alloc]init];
        minView.frame = CGRectMake(0,ScreenWidth*7/15+9.f +10 , ScreenWidth, 70);
        [cell.contentView addSubview:minView];
        //    [minView mas_makeConstraints:^(MASConstraintMaker *make) {
        //        make.left.right.equalTo (weakSelf);
        ////        make.top.equalTo (_topScrollerview.mas_bottom).offset (9.f);
        //        make.top.equalTo (@ScreenWidth*7/15+9.f);
        //        make.height.equalTo (@(70));
        //    }];
        
        
        CGFloat width = ScreenWidth/4;
        CGFloat btnWidth = width -20;
        
      NSArray *  _lableArr = USER_DEFAULTGet(@"className");
      NSArray *  _imageArr = USER_DEFAULTGet(@"classimage");
        
        
        for (int i = 0; i < _lableArr.count; i ++) {

            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(btnWidth * i + 20*i +10, 0, btnWidth, 50);
            [btn sd_setBackgroundImageWithURL:ImageUrl(_imageArr[i]) forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag = 1000 +i;
            [minView addSubview:btn];
            
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(width*i, 60, width, 20)];
            label.text = _lableArr[i];
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont fontWithName:@"PingFang-SC-Bold" size:14];
            label.textColor = RGBA(51, 51, 51, 1);
            label.textColor = RGBA(51, 51, 51, 1);
            [minView addSubview:label];
        }
        

        
//        HomeTopCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeTopCell" forIndexPath:indexPath];
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//
//
//
//        cell.categoryBlock = ^(NSString *title) {
//
//            weakSelf.navTitle = title;
//            NSString * classIdStr;
//            for (NSDictionary * dic in categoryArray) {
//
//                if ([title isEqualToString:dic[@"class_name"]]) {
//                    classIdStr = dic[@"id"];
//                }
//            }
        
//            [weakSelf categoryBtnJumdForClassId:classIdStr];
//        };
        return cell;
    }
    else{
        
        
        NSDictionary * dic = _dataArray[indexPath.row];
        NSArray * imageArray = dic[@"cover_pic"];
        
        if (imageArray.count!=1) {
            
            static NSString * cellid = @"cellNews";
            
            UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellid];
            if (cell==nil) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellid];
            }else {
                
                while ([cell.contentView.subviews lastObject] !=nil) {
                    
                    [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
                }
            }
            
            UILabel * titleLabel  = [[UILabel alloc]initWithFrame:CGRectMake(10, 15, WIDTH -20, 20)];
            titleLabel.text = dic[@"title"];
            titleLabel.font = [UIFont systemFontOfSize:16];
            titleLabel.numberOfLines = 2;
            [titleLabel sizeToFit];
            
            [cell.contentView addSubview:titleLabel];
            CGFloat widthImage = (WIDTH -40)/3;
            UIImageView * image;
            
            
            
            if (imageArray.count==0) {
                
                image = [[UIImageView alloc]initWithFrame:CGRectMake(10,CGRectGetMaxY(titleLabel.frame)+10 ,10 ,1)];
            }else {
                
                for (int i = 0; i<imageArray.count; i++) {
                    
                    image = [[UIImageView alloc]initWithFrame:CGRectMake(10+widthImage*i+10*i, CGRectGetMaxY(titleLabel.frame)+10, widthImage, widthImage *0.6)];
                    [image sd_setImageWithURL:ImageUrl(imageArray[i])];
                    image.layer.cornerRadius = 3.f;
                    image.layer.masksToBounds = YES;
                    [cell.contentView addSubview:image];
                }
                
            }
            
            UIImageView * messageView  =[[UIImageView alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(image.frame)+10, 18, 18)];
            messageView.image = ImageName(@"home_comment_default");
            [cell.contentView addSubview:messageView];
            
            UILabel * comment = [[UILabel alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(image.frame)+10, 50, 18)];
            comment.font = [UIFont systemFontOfSize:13];
            comment.text= [NSString stringWithFormat:@"%ld",[dic[@"comment_num"] integerValue]];
            comment.textColor  = [UIColor lightGrayColor];
            comment .numberOfLines = 1;
            [comment sizeToFit];
            
            [cell.contentView addSubview:comment];
            
            UIImageView * lookView  =[[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(comment.frame)+10, CGRectGetMaxY(image.frame)+10, 18, 18)];
            lookView.image = ImageName(@"home_watch_default");
            [cell.contentView addSubview:lookView];
            
            UILabel * look = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(lookView.frame)+3, CGRectGetMaxY(image.frame)+10, 50, 18)];
            look.font = [UIFont systemFontOfSize:13];
            look.text= [NSString stringWithFormat:@"%ld",[dic[@"view_num"] integerValue]];
            look.textColor  = [UIColor lightGrayColor];
            look .numberOfLines = 1;
            [look sizeToFit];
            [cell.contentView addSubview:look];
            
            UILabel * time = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH -150, CGRectGetMaxY(image.frame)+10, 140, 18)];
            time.font = [UIFont systemFontOfSize:13];
            time.text= [NSString stringWithFormat:@"%@",dic[@"create_time"]];
            time.textColor  = [UIColor lightGrayColor];
            time.textAlignment = NSTextAlignmentRight;
          
            [cell.contentView addSubview:time];
            
            UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(time.frame)+10, WIDTH -20, 0.5)];
            lineView.backgroundColor  = [UIColor groupTableViewBackgroundColor];
            [cell.contentView addSubview:lineView];
            return cell;
            
            
        }else {  // 单图
    SHNesHomeTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"SHNesHomeTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
        [cell.image sd_setImageWithURL:ImageUrl(dic[@"cover_pic"][0])];
            
            if (ISP5||ISP4) {
                 cell.image.frame = CGRectMake(10, (100 -((WIDTH -40)/3 * 0.7))/2, (WIDTH -40)/3, (WIDTH -40)/3 * 0.7);
            }else {
            cell.image.frame = CGRectMake(10, (100 -((WIDTH -40)/3 * 0.6))/2, (WIDTH -40)/3, (WIDTH -40)/3 * 0.6);
            }
        cell.image.layer.cornerRadius = 3.f;
        cell.image.layer.masksToBounds = YES;
        cell.commentNumber.text = [NSString stringWithFormat:@"%ld",[dic[@"comment_num"] integerValue]];
            
            if (ISP5||ISP4) {
                 cell.titleLabel.frame = CGRectMake((WIDTH -40)/3 +20, 15, WIDTH -((WIDTH -40)/3-5)-20, 20);
            }else{
              cell.titleLabel.frame = CGRectMake((WIDTH -40)/3 +20, 12, WIDTH -((WIDTH -40)/3-5)-20, 20);
            }
        cell.titleLabel.text  = dic[@"title"];
        cell.titleLabel.numberOfLines = 2;
        [cell.titleLabel sizeToFit];
       
        cell.commentNumber.numberOfLines = 1;
        [cell.commentNumber sizeToFit];
            
            cell.commentImage.frame = CGRectMake((WIDTH -40)/3+20, 70, 18, 18);
            cell.commentNumber.frame  = CGRectMake((WIDTH -40)/3 + 40, 70, 18, 18);
        cell.lookImage.frame = CGRectMake(CGRectGetMaxX(cell.commentNumber.frame)+10, 70, 18, 18);
        
        
        cell.timeLabel.frame = CGRectMake(CGRectGetMaxX(cell.lookImage.frame)+3, 70, WIDTH - (CGRectGetMaxX(cell.lookImage.frame)+10), 18);
            
            if (ISP5||ISP4) {
                cell.time.hidden =YES;
                
                cell.timeLabel.text = [NSString stringWithFormat:@"%ld  %@",[dic[@"view_num"] integerValue],dic[@"create_time"]];
            }else {
                
                cell.timeLabel.text = [NSString stringWithFormat:@"%ld",[dic[@"view_num"] integerValue]];
            cell.time.frame =  CGRectMake(WIDTH -150, CGRectGetMaxY(cell.timeLabel.frame)-18, 140, 18);
           
            cell.time.text= [NSString stringWithFormat:@"%@",dic[@"create_time"]];
           
             cell.time.textAlignment = NSTextAlignmentRight;
        
            }
            
            UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(cell.contentView.frame)-0.5, WIDTH -20, 0.5)];
            lineView.backgroundColor  = [UIColor groupTableViewBackgroundColor];
            [cell.contentView addSubview:lineView];
        
      [cell setViewBottomShadow];
     return cell;
        }
    }
}

-(void)btnClicked:(UIButton *)btn{
    
    
    
    NSMutableArray * className = @[].mutableCopy;
    if (className) {
        [className removeAllObjects];
    }
  
    for (NSDictionary * dic in categoryArray) {
        [className addObject:dic[@"class_name"]];
    }
    
    NSString * title = className[btn.tag - 1000]
    ;
    
    NSString * classIdStr;
       for (NSDictionary * dic in categoryArray) {
    
                    if ([title isEqualToString:dic[@"class_name"]]) {
                        
                        classIdStr = dic[@"id"];
                        
                        [self categoryBtnJumdForClassId:classIdStr andTitle:title];
                    }

           
            };

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 1) {
        NSDictionary * dic = _dataArray[indexPath.row];
        NewsDetailsViewController * homeDetiel = [[NewsDetailsViewController alloc]init];
        homeDetiel.news_idStr = [NSString stringWithFormat:@"%ld",[dic[@"id"] integerValue]];
        homeDetiel.hidesBottomBarWhenPushed = YES;
        
        [self.navigationController pushViewController:homeDetiel animated:YES];
    }else{
        return;
    }
    
}

-(void)categoryBtnJumdForClassId:(NSString *)classid andTitle:(NSString *)title{
   HomeCategoryVC * destinationVC = [[HomeCategoryVC alloc] init];
    destinationVC.navTitle = title;
    destinationVC.classID = classid;
    destinationVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:destinationVC animated:YES];

}
-(void)getMainListRequest{
    
    NSDictionary * dic = @{@"page":[NSString stringWithFormat:@"%d",page],
                           };
    
    if ([typeStr isEqualToString:@"hot"]) {
        dic = @{@"page":[NSString stringWithFormat:@"%d",page],
                @"hot":@"DESC"
                };
    }else{
        
        dic = @{@"page":[NSString stringWithFormat:@"%d",page],
                @"comment":@"DESC"
                };
    }
    [PostNetWorking postNetWorkingStr:RequestUrlStr(news_list_api) andparameters:dic Success:^(id responseObject) {
        
        if ([responseObject isKindOfClass:[NSArray class]]) {
            NSArray * array = responseObject;
            
            if (page == 1) {
                if (_dataArray) {
                    [_dataArray removeAllObjects];
                }
                for (NSDictionary * dic in array) {
                    
                    [_dataArray addObject:dic];
                }
            }else {
                for (NSDictionary * dic in array) {
                    
                    [_dataArray addObject:dic];
                }
            }
            
            [self.bottomTab reloadData];
        }else{
            
            SHuiMBhudTextTimeStr(responseObject[@"msg"]);
        }
        
        [self.bottomTab.mj_header endRefreshing];
        [self.bottomTab.mj_footer endRefreshing];
        NSLog(@"responseObject = %@",responseObject);
        
    } failed:^(NSURLSessionDataTask * _Nullable task,NSError *error) {
        [self.bottomTab.mj_header endRefreshing];
        [self.bottomTab.mj_footer endRefreshing];
        NSHTTPURLResponse * responses = (NSHTTPURLResponse *)task.response;
        if (responses.statusCode == 404) {
            
            [self.bottomTab.mj_footer setState:MJRefreshStateNoMoreData];
        }else {
            SHuiMBhudTextNetError;
        }
    }];
    
}

-(void)getCategoryDataRequest{
    

    [PostNetWorking netWorkingStr:RequestUrlStr(main_class_api)  Success:^(id responseObject) {
        
        if ([responseObject isKindOfClass:[NSArray class]]) {
            NSArray * array = responseObject;
        
             categoryArray = array;
            
            NSMutableArray * className = @[].mutableCopy;
            if (className) {
                [className removeAllObjects];
            }
            NSMutableArray * classimage = @[].mutableCopy;
            if (classimage) {
                [classimage removeAllObjects];
            }
            for (NSDictionary * dic in categoryArray) {
                [className addObject:dic[@"class_name"]];
                [classimage addObject:dic[@"class_pic"]];
            }
            USER_DEFAULTSet(className, @"className");
            USER_DEFAULTSet(classimage, @"classimage");
            dispatch_async(dispatch_get_main_queue(), ^{
                 [self.bottomTab reloadData];
            });
           
        }else{
            
            SHuiMBhudTextTimeStr(responseObject[@"msg"]);
        }
        NSLog(@"responseObject = %@",responseObject);
        
    } failed:^(NSURLSessionDataTask * _Nullable task,NSError *error) {
        
        SHuiMBhudTextNetError;
    }];
    
}

-(void)getBannerDataRequest{
    
    
    [PostNetWorking netWorkingStr:RequestUrlStr(get_banner_api)  Success:^(id responseObject) {
        
        if ([responseObject isKindOfClass:[NSArray class]]) {
            NSArray * array = responseObject;
            
            bannerArr = array;
            
            USER_DEFAULTSet(bannerArr , @"bannerArr");
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.bottomTab reloadData];
            });
        }else{
            
            SHuiMBhudTextTimeStr(responseObject[@"msg"]);
        }
        NSLog(@"responseObject = %@",responseObject);
        
            
        
    } failed:^(NSURLSessionDataTask * _Nullable task,NSError *error) {
        
        SHuiMBhudTextNetError;
    }];
    
}

// 随手拍详情
-(void)getActDetailsRequestForid:(NSString *)newsId{
    
    NSString * user_id = [NSString stringWithFormat:@"%ld",[USER_DEFAULTGet(@"user_id")  integerValue]];
    if (user_id.length>1) {
        
    }else {
        
        user_id = @"";
    }
   
    NSDictionary * dic = @{@"news_id":newsId,
                           @"user_id":user_id
                           };
    
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(activity_details_api) andparameters:dic Success:^(id responseObject) {
        
        NSDictionary *  dataDic = responseObject;
        NSLog(@"responseObject = %@",responseObject);
        
        USER_DEFAULTSet(dataDic[@"pic_url"], @"actDetailsIamgeArray");
        USER_DEFAULTSet(dataDic[@"is_fabulous"], @"act_is_fabulous");
        
        
        if ([dataDic[@"pic_url"] isKindOfClass:[NSArray class]]) {
            NSArray * array = dataDic[@"pic_url"];
            if (array.count==0) {
                
                USER_DEFAULTSet(@"http://a1.qpic.cn/psb?/V147v55d35m6eX/xXdZ2De4s1iE5gYJaikuWyqltDsmFf*LHHZ1ANFPtso!/m/dDABAAAAAAAA&bo=eAB4AAAAAAADByI!&rf=photolist", @"shareImage");
            }else {
                
                USER_DEFAULTSet(dataDic[@"pic_url"][0], @"shareImage");
            }
        }
        NSString * newsId= [NSString stringWithFormat:@"%ld",[dataDic[@"id"] integerValue]];
        USER_DEFAULTSet(newsId,@"shareNewsId");
        USER_DEFAULTSet(@"2",@"shareType");
        USER_DEFAULTSet(dataDic[@"title"], @"shareTitle");
        USER_DEFAULTSet(dataDic[@"content"], @"shareContent");
        USER_DEFAULTSet(dataDic[@"share_url"], @"shareUrl");
        
        
        ActiveDetailViewController * activeDetail = [[ActiveDetailViewController alloc]init];
        activeDetail.actIdStr = newsId;
        activeDetail.dataDic = dataDic;
        activeDetail.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:activeDetail animated:YES];
        
        
    } failed:^(NSURLSessionDataTask * _Nullable task,NSError *error) {
        
        NSHTTPURLResponse * responses = (NSHTTPURLResponse *)task.response;
        if (responses.statusCode == 404) {
            SHuiMBhudTextTimeStr(@"未获取到数据");
        }else {
            SHuiMBhudTextNetError;
        }
    }];
    
}

@end
